package com.ingenio.siot.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.ingenio.siot.adapter.ParametroAdapter;
import com.ingenio.siot.entity.Parametro;

/**
 * Servlet implementation class DevolverParametroServlet
 */
@WebServlet("/DevolverParametro")
public class DevolverParametroServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DevolverParametroServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			JSONObject json = new JSONObject();
			if(request.getParameter("param") != null){
				String parametro = request.getParameter("param").trim();
				ParametroAdapter pa = new ParametroAdapter();
				Parametro p = pa.devolverParametro(parametro);
				if(p != null){
				  json.put("valor", p.getValor());
				}
				else {
				  json.put("error", "No recupera parametro");
				}
			}else {
				json.put("error", "error parametro");
			}
			response.setContentType("application/json");
			response.getWriter().write(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
